/**
 * 观察者模式
 * 事件模式
 */

class Event {
    constructor() {
        this._starts = []
        this._ons = []
        this._ends = []
    }

    on(key, func) {
        switch (key) {
            case 'start':
                this._starts.push(func)
                break;
            case 'data':
                this._ons.push(func)
                break;
            case 'end':
                this._ends.push(func)
                break;
            default:
                break;
        }
    }

    trans() {
        this.notify('_starts', null)
        for (let i = 1; i < 100; i++) {
            this.notify('_ons', i)
        }
        this.notify('_ends', null)
    }

    notify(stage, data) {
        this[stage].forEach(v => v(data))
    }
}

const event = new Event()

event.on('start', () => {
    console.log('starting')
})

event.on('end', () => {
    console.log('ending')
})

event.on('data', (data) => {
    console.log(data)
})

event.trans()



